package offer_test_40;

import java.util.Arrays;

//输入一个正整数数组，把数组里所有数字拼接起来排成一个数，
//打印能拼接出的所有数字的最小的一个。例如输入{3,32,321}，则打印最小的数字是321323.
public class thirteen_SortArrayForMinNum {
	public static void main(String[] args) {
		int[] array={3,32,321};
		   quicksort(array,0,array.length -1);  
	        System.out.println(Arrays.toString(array));  
	}
	public static int sort(int[] array,int head,int tail){
		int mid=array[head];
		while(head<tail){
			while (head < tail && judgeMin(mid, array[tail])) {
				tail--;
			}
			array[head] = array[tail];
			while (head < tail && judgeMin(array[head],mid )) {
				head++;
			}
			array[tail] = array[head];
		}
		array[head] = mid;
		return head;
	}
	public static void  quicksort(int[] array,int head,int tail){
		  int q;  
	        if(head<tail){  
	             q = sort(array,head,tail);  
	             quicksort(array,q+1,tail);  
	             quicksort(array,tail,q-1);  
	    }  
	}
	public static boolean judgeMin(int a, int b) {
		String left = (a+"") + (b+"");
		String right = (b+"") + (a+"");
		boolean result=false;
		for (int i = 0; i < left.length(); i++) {
			if (left.charAt(i) < right.charAt(i))
				return true;
			else if (left.charAt(i) > right.charAt(i))
				return false;;
		}
		return result;
	}
}